﻿#include "setuptotalwind.h"
#include "ui_setuptotalwind.h"
#include <mainwindow.h>
extern intListener doorStatus;
extern intListener passwordfl;

setuptotalwind::setuptotalwind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setuptotalwind)
{
    mpShadeWindow = new QWidget(this);
    ui->setupUi(this);
    connect(&doorStatus, &intListener::valueChanged,this,&setuptotalwind::doorStatusChanged,Qt::QueuedConnection);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 20); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->pushButton->setFont(font);
            ui->pushButton_2->setFont(font);
            ui->pushButton_3->setFont(font);
            ui->pushButton_7->setFont(font);
            ui->pushButton_8->setFont(font);
            ui->pushButton_9->setFont(font);
            ui->pushButton_10->setFont(font);
            ui->pushButton_11->setFont(font);
            ui->pushButton_12->setFont(font);
            ui->pushButton_12->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnMainBtn->setFont(familyName);
            ui->pushButton_5->setFont(familyName);
        }
    }
#endif
}
void setuptotalwind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_5->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_5->setText("        关闭仓门");
        ui->pushButton_5->setText("        关闭仓门");
    }
}

setuptotalwind::~setuptotalwind()
{
    delete ui;
}
void setuptotalwind::showEvent(QShowEvent *event)
{
    tcpClient = TcpClient::getInstance();

    if(doorStatus.get()==0)
    {
        ui->pushButton_5->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_5->setText("        关闭仓门");
        ui->pushButton_5->setText("        关闭仓门");
    }
    QObject::connect(&passwordfl, &intListener::valueChanged, this,&setuptotalwind::passwordchange);

    refShadeWindow();
    event->accept();
}

void setuptotalwind::resizeEvent(QResizeEvent *event){
    refShadeWindow();
}

void setuptotalwind::refShadeWindow(){
    if(mpShadeWindow){
        mpShadeWindow->close();
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select * From IsFirstStartUp";
    query.exec(sql);

    bool isFirst=false;
    int guideStatus;
    while (query.next()) {
        isFirst = query.value("IsFirst").toString()=="0";
        guideStatus = query.value("GuideStatus").toInt();
    }
    query.clear();

    if(isFirst){
        if(guideStatus==0)//网络连接
        {
            mpShadeWindow = new QWidget(this);
            QString str("QWidget{background-color:rgba(0,0,0,0.6);}");   //0.6代表透明度，根据需要自行调整
            mpShadeWindow->setStyleSheet(str);
            QRect rect = this->geometry();
            int screenWidth = rect.width();  // 屏幕宽度
            int screenHeight = rect.height();  // 屏幕高度
            mpShadeWindow->setGeometry(0, 0,screenWidth,screenHeight);    //遮罩窗口位置

            QRect btnRect= ui->pushButton_7->frameGeometry();

            QLabel *label=new QLabel("1/4网络连接",mpShadeWindow);
            QPalette palette;
            palette.setColor(QPalette::WindowText, QColor(255,255,255));
            QFont font=label->font();
            font.setPointSize(24);
            label->setFont(font);
            label->setPalette(palette);

#ifdef Q_OS_ANDROID
            label->setGeometry(btnRect.x()+40,screenHeight-470,180,30);

#else
            label->setGeometry(btnRect.x()+40,screenHeight-520,180,30);
#endif
            label->setStyleSheet("background-color:rgba(0,0,0,0);");

            QPushButton * btnSkipThis = new QPushButton("跳过当前引导",mpShadeWindow);

#ifdef Q_OS_ANDROID
            btnSkipThis->setGeometry(btnRect.x()+10,screenHeight-420,120,40);

#else
            btnSkipThis->setGeometry(btnRect.x()+10,screenHeight-470,120,40);
#endif
            btnSkipThis->setStyleSheet("border-radius: 8px; background-color:#FFFFFF");

            QObject::connect(btnSkipThis, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set GuideStatus=1";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton * btnSkipAll = new QPushButton("跳过全部引导",mpShadeWindow);

#ifdef Q_OS_ANDROID
            btnSkipAll->setGeometry(btnRect.x()+140,screenHeight-420,120,40);

#else
            btnSkipAll->setGeometry(btnRect.x()+140,screenHeight-470,120,40);
#endif
            btnSkipAll->setStyleSheet("border-radius: 8px; background-color:#2F42CD");

            QObject::connect(btnSkipAll, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set IsFirst='1',GuideStatus=5";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton *btnSet=new QPushButton("        网络连接",mpShadeWindow);

#ifdef Q_OS_ANDROID
            btnSet->setGeometry(btnRect.x()+10,screenHeight-365,256,80);
#else
            btnSet->setGeometry(btnRect.x()+10,screenHeight-420,256,80);
#endif

            QFont btnFont=btnSet->font();
            font.setPointSize(16);
            btnSet->setFont(font);
            btnSet->setStyleSheet("border-image: url(:/pic/picture/s5.png);background-color:rgba(0,0,0,0);");

            QLabel *movieLabel=new QLabel("",mpShadeWindow);

#ifdef Q_OS_ANDROID
            movieLabel->setGeometry(btnRect.x()+80,screenHeight-330,60,60);
#else
            movieLabel->setGeometry(btnRect.x()+80,screenHeight-380,60,60);
#endif

            movieLabel->setStyleSheet("background-color:rgba(0,0,0,0);");

            QMovie  *guideMovie = new QMovie(":/pic/picture/guide.gif");
            movieLabel->setMovie(guideMovie);
            guideMovie->start();

            QObject::connect(btnSet, &QPushButton::clicked,[this](){
                this->hide();
                emit changeSetUp5Window();
            });

            mpShadeWindow->show();
        }
    }
}

void setuptotalwind::on_returnMainBtn_clicked()
{
    this->close();
    emit changeMainWindow();
}

void setuptotalwind::on_pushButton_clicked()
{
    this->close();
    emit changeSetUp1Window();
}

void setuptotalwind::on_pushButton_2_clicked()
{
    this->close();
    emit changeSetUp2Window();
}

void setuptotalwind::on_pushButton_3_clicked()
{
    this->close();
    emit changeSetUp3Window();
}

void setuptotalwind::on_pushButton_6_clicked()
{
    this->close();
    emit changeSetUp4Window();
}

void setuptotalwind::on_pushButton_7_clicked()
{
    this->close();
    emit changeSetUp5Window();
}

void setuptotalwind::on_pushButton_8_clicked()
{
    this->close();
    emit changeSetUp6Window();
}

void setuptotalwind::passwordchange()
{
    if(MainWindow::passWordType==1 && passwordfl.get()==1){
        this->hide();
        passwordfl.set(0);
    }
}

void setuptotalwind::on_pushButton_9_clicked()
{
    this->close();
    emit changeSetUp7Window();
}

void setuptotalwind::on_pushButton_10_clicked()
{
    // 创建一个QWidget作为容器
    QWidget *container = new QWidget;
    container->setAttribute(Qt::WA_QuitOnClose, false);
    container->setWindowIcon(QIcon(":/pic/picture/96favicon.ico"));
    container->setWindowTitle("关于我们");

    container->setStyleSheet("background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #007bd8,stop:0.5 #474ac1 ,stop:1 #001a5a)");
    // 创建一个QLabel
    QLabel *label = new QLabel(container); // 设置container为parent
    label->setMinimumSize(500, 400);
    label->setWordWrap(true); // 启用自动换行
    // 设置字号（字体大小）
    QFont font = label->font();
    font.setPointSize(12); // 设置字体大小为 12 点
    label->setFont(font);
    label->setStyleSheet("QLabel { background: rgba(255,255,255,0.76); }");
    label->setAlignment(Qt::AlignCenter); // 这将使文本在 QLabel 内部居中

    label->setText("    软件名称:RT-PCR-96 Analysis software \n 软件版本:V1.4 \n 公司名称:长春市博坤生物科技有限公司\n"
                   "声明：仅用于研究，不能用于生物诊断");

    // 创建一个QPushButton
    QPushButton *button = new QPushButton("返回", container); // 设置container为parent
    font = button->font();
    font.setPointSize(12); // 设置字体大小为 12 点
    button->setStyleSheet("QPushButton { background: rgba(255,255,255,0.76); }");
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            label->setFont(font);
            button->setFont(font);
        }
    }
#endif

    // 创建一个垂直布局
    QVBoxLayout *layout = new QVBoxLayout(container);

    // 将QLabel和QPushButton添加到布局中
    layout->addWidget(label); // label将占据大部分空间
    layout->addWidget(button); // button将显示在label下方

    // 设置container的布局为刚才创建的QVBoxLayout（这步其实已经在new QVBoxLayout(container)时完成了）

    // 如果container是某个窗口或对话框的子控件，你需要将其显示出来
    // 连接按钮的clicked信号到QWidget的close槽
    connect(button, &QPushButton::clicked, container, &QWidget::close);
    connect(ui->returnMainBtn, &QPushButton::clicked, container, &QWidget::close);
    container->show(); // 显示container，label和button会自动显示，因为它们是container的子控件
}

void setuptotalwind::on_pushButton_11_clicked()
{
    helpfileui = new helpfile();
    helpfileui->show();
}

void setuptotalwind::on_pushButton_5_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_5->setText("        关闭仓门");
        ui->pushButton_5->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_5->setText("        打开仓门");
        ui->pushButton_5->setText("        打开仓门");
    }
}

void setuptotalwind::on_pushButton_12_clicked()
{
    MainWindow::passWordType=1;
    emit changeMaintainWindow();
}
